package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.ShippingAddress;
import com.example.demo.entity.User;
import com.example.demo.exception.ApplicationException;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.ShippingAddressService;
import com.example.demo.mapper.ShippingAddressMapper;
import com.example.demo.vo.Result;
import com.example.demo.vo.ResultCode;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 15085202591
* @description 针对表【shipping_address】的数据库操作Service实现
* @createDate 2024-05-29 15:32:26
*/
@Slf4j
@Service
public class ShippingAddressServiceImpl extends ServiceImpl<ShippingAddressMapper, ShippingAddress> implements ShippingAddressService{

    @Resource
    private ShippingAddressMapper addressMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<ShippingAddress> selectAllAddressByUid(Long id) {
        // 1. 参数校验
        if (id == null || id < 1) {
            log.warn("参数校验失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 根据uid得到用户信息,判断是否合理
        User existUser = userMapper.selectById(id);
        if (existUser == null || existUser.getIsDeleted() == 1) {
            log.warn("用户不存在!");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0005));
        }
        if (existUser.getStatus() == 0) {
            log.warn("用户状态异常");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0006));
        }

        // 3. 调用mapper查出地址集合
        List<ShippingAddress> addresses = addressMapper.selectList(
                new QueryWrapper<ShippingAddress>().eq("uid", id)
        );
        log.info("查询成功");

        // 4. 返回结果
        return addresses;
    }


    @Override
    public void addShippingAddress(ShippingAddress address) {
        // 1. 校验参数
        if (address == null) {
            log.warn("收货地址对象不能为空");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 根据用户id查出用户对象
        User existUser = userMapper.selectById(address.getUid());
        if (existUser == null || existUser.getIsDeleted() == 1) {
            log.warn("用户不存在!");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0005));
        }
        if (existUser.getStatus() == 0) {
            log.warn("用户状态异常");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0006));
        }

        // 3. 设置时间
        LocalDateTime dateTime = LocalDateTime.now();
        address.setCreate_time(dateTime);
        address.setUpdate_time(dateTime);

        // 4. 调用mapper
        int row = addressMapper.insert(address);

        // 验证操作是否成功
        if (row != 1) {
            log.warn("新增收货地址失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0008));
        }

        log.info("新增收货地址成功");

    }

    @Override
    public void modifyShippingAddress(ShippingAddress address) {
        // 1. 校验参数
        if (address == null) {
            log.warn("收货地址对象不能为空");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 设置时间
        LocalDateTime dateTime = LocalDateTime.now();
        address.setCreate_time(dateTime);
        address.setUpdate_time(dateTime);

        // 3. 调用mapper
        UpdateWrapper<ShippingAddress> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", address.getId());
        int row = addressMapper.update(address, wrapper);

        // 验证操作是否成功
        if (row != 1) {
            log.warn("修改收货地址失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0009));
        }

        log.info("修改收货地址成功");
    }

    @Override
    public ShippingAddress selectAddressById(Long id) {
        // 1. 参数校验
        if (id == null || id < 1) {
            log.warn("非法收货地址id");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 调用mapper
        ShippingAddress address = addressMapper.selectById(id);


        // 3. 成功返回
        log.info("成功查询一条收货地址");
        return address;

    }

    @Override
    public void deleteAddressById(Long id) {
        // 1. 校验参数
        if (id == null || id < 1) {
            log.warn("非法收货地址id");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 调用mapper
        int row = addressMapper.deleteById(id);

        // 3. 校验返回
        if (row != 1) {
            log.warn("收货地址删除失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0010));
        }

        log.info("删除收货地址成功");

    }

    @Override
    public void updateSelectedById(Long id, Long uid) {
        addressMapper.updateSelectedById(id,uid);
    }

    @Override
    public Long getIDBySelected(Long uid) {

        return addressMapper.getIDBySelected(uid);
    }
}




